package is.glif.fenius.webui.servlet;


import is.glif.fenius.webui.topo.FileNetworkProvider;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class NetworkServlet extends HttpServlet {
	private static Logger log = Logger.getLogger(NetworkServlet.class);
    private static final long serialVersionUID = 1L;
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        PrintWriter pw = response.getWriter();
        String name = request.getParameter("name");
        try {
            JSONArray arr = FileNetworkProvider.getInstance().getNetworks(name);
            JSONObject obj = new JSONObject();
            obj.put("items", arr);
            obj.put("identifier", "id");
            obj.put("label", "name");
            
            
            String output = obj.toString(2);
            pw.println(output);
            // System.out.println(output);
        } catch (JSONException e) {
            pw.println("Error: " + e.getMessage());
            e.printStackTrace();
        }
     }

}
